<template>
  <a-layout>
    <a-layout-sider width="200" style="background: #fff">
      <a-menu
          v-model:selectedKeys="selectedKeys2"
          v-model:openKeys="openKeys"
          mode="inline"
          :style="{ height: '100%', borderRight: 0 }"
      >
        <a-sub-menu key="sub1">
          <template #title>
              <span>
                <user-outlined/>
                subnav 1
              </span>
          </template>
          <a-menu-item key="1">option1</a-menu-item>
          <a-menu-item key="2">option2</a-menu-item>
          <a-menu-item key="3">option3</a-menu-item>
          <a-menu-item key="4">option4</a-menu-item>
        </a-sub-menu>
        <a-sub-menu key="sub2">
          <template #title>
              <span>
                <laptop-outlined/>
                subnav 2
              </span>
          </template>
          <a-menu-item key="5">option5</a-menu-item>
          <a-menu-item key="6">option6</a-menu-item>
          <a-menu-item key="7">option7</a-menu-item>
          <a-menu-item key="8">option8</a-menu-item>
        </a-sub-menu>
        <a-sub-menu key="sub3">
          <template #title>
              <span>
                <notification-outlined/>
                subnav 3
              </span>
          </template>
          <a-menu-item key="9">option9</a-menu-item>
          <a-menu-item key="10">option10</a-menu-item>
          <a-menu-item key="11">option11</a-menu-item>
          <a-menu-item key="12">option12</a-menu-item>
        </a-sub-menu>
      </a-menu>
    </a-layout-sider>
    <a-layout style="padding: 0 24px 24px">
      <a-layout-content
          :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
      >
        <a-button type="primary" @click="add">添加</a-button>
        <a-table :dataSource="newsList"
                 :columns="columns"
                 :pagination="pagination"
                 :loading="Loading"
                 @change="handleTableChange"
        >
          <template v-slot:action="{text,record}">
            <a-button type="primary" @click="edit(record)">编辑</a-button>
            <a-popconfirm
                title="删除后无法恢复，确认删除?"
                ok-text="是"
                cancel-text="否"
                @confirm="del(record.id)"
            >
              <a-button type="danger">删除</a-button>
            </a-popconfirm>

          </template>
        </a-table>
      </a-layout-content>
    </a-layout>
  </a-layout>

  <a-modal v-model:visible="modelVisible"
           title="新闻编辑表单"
           cancel-text="取消"
           ok-text="确定"
           @ok="handleModelOk">
    <a-form
        :model="news"
        :label-col="{ span: 6 }"
        :wrapper-col="{ span: 16 }"
    >

      <a-form-item label="新闻类别">
        <a-select v-model:value="news.categoryid">
            <a-select-option v-for="category in categoryList" :value="category.id" :key="category.id">
              {{category.cname}}
            </a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item label="标题">
        <a-input v-model:value="news.title" />
      </a-form-item>

      <a-form-item label="新闻摘要">
        <a-input v-model:value="news.summary" />
      </a-form-item>

      <a-form-item label="作者">
        <a-input v-model:value="news.author" />
      </a-form-item>

    </a-form>
  </a-modal>
</template>

<script lang="ts">
//ref:vue3中model和view双向绑定间的中介
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import {Tool} from "@/util/tool";

export default defineComponent({
  name: "news-list",

  //data,methods,created合体
  setup() {

    const newsList = ref();
    const news=ref({});
    const categoryList=ref();

    const columns = [
      {
        title: '标题',
        dataIndex: 'title',
        key: 'title',
        width:200
      },
      {
        title: '摘要',
        dataIndex: 'summary',
        key: 'summary',
        width:800
      },
      {
        title: '作者',
        dataIndex: 'author',
        key: 'author',
        width:100
      },
      {
        title: 'Action',
        key: 'action',
        slots:{customRender:'action'},
        width:150
      }
    ];

    const handleQueryCategory = () => {
      axios.get('http://localhost:8899/news-query/findCategoryList').then(response=>{
        categoryList.value=response.data;
      });
    };

    //添加或编辑
    const handleModelOk = () => {
      axios.post('http://localhost:8899/news-edit/saveNews',news.value).then(response=>{
        modelVisible.value=false;  //关闭窗口
        //加载最新新闻列表
        handleQueryNewsList({
          page:pagination.value.current,
          size:pagination.value.pageSize
            });
      })
    };

    const add = () => {
      modelVisible.value=true;
      //初始化新闻对象
      news.value={};
    };
    
    const edit = (record:any) => {
      modelVisible.value=true;
      news.value=Tool.copy(record); //复制对象
    }

    const pagination = ref({
      current:1,
      pageSize:4,
      total:0
    });

    const loading = ref<boolean>(false);
    const modelVisible = ref<boolean>(false);


    //提交删除
    const del = (id:number) => {
      axios.delete('http://localhost:8899/news-edit/deleteNews/'+id).then(response=>{
        //加载最新新闻列表
        handleQueryNewsList({
          page:pagination.value.current,
          size:pagination.value.pageSize
        });
      })
    };


    const handleQueryNewsList = (params:any) => {
      loading.value = true;
      axios.get('http://localhost:8899/news-query/listByPage',{
        params:{
          page:params.page,
          size:params.size
        }
      }).then((response) => {
        loading.value = false;
        newsList.value = response.data.list;
        //重置分页按钮
        pagination.value.current = params.page;
        pagination.value.total = response.data.total;

      });
    };

    const handleTableChange=(pagination:any)=>{
      handleQueryNewsList({
        page:pagination.current,
        size:pagination.pageSize
      })
    }

    onMounted(() => {
      handleQueryNewsList({
        page:1,
        size:pagination.value.pageSize
      });
      handleQueryCategory();
    });

    return {
      newsList,
      handleQueryNewsList,
      columns,
      pagination,
      handleTableChange,
      loading,
      add,
      modelVisible,
      handleModelOk,
      news,
      categoryList,
      handleQueryCategory,
      edit,
      del
    }
  }
})
</script>

<style scoped>

</style>